C++ Pointers

03-11-17 Course- CPP

Pointers are the powerful feature of C++ programming which differs it from other popular programming languages like: Java, Visual Basic etc.

To understand pointers, you should have the knowledge of address in computer memory. Computer memory is broken down into bytes and each byte has its own address. For example: In 1KB memory, there are 1024 bytes and each byte is given an address (0 - 1023).

The Address-of Operator &

The & operator can find address occupied by a variable. If var is a variable then, &var gives the address of that variable.

Example 1: Address-of Operator


#include <iostream>
using namespace std;

int main() {
    int var1 = 3;
    int var2 = 24;
    int var3 = 17;
    cout<<&var1<<endl;
    cout<<&var2<<endl;
    cout<<&var3<<endl;

}

Output


0x7fff5fbff8ac
0x7fff5fbff8a8
0x7fff5fbff8a4

The 0x in the beginning represents the address is in hexadecimal form. (You may not get the same result on your system.). Notice that first address differs from second by 4-bytes and second address differs from third by 4-bytes. It is because the size of integer(variable of type int) is 4 bytes in 64-bit system.

Pointers Variables

Now you know about address in computer memory, it's time to learn about pointers.

Consider a normal variable as in above example, these variables holds data. But pointer variables or simply pointers are the special types of variable that holds memory address instead of data.

How to declare a pointer?


int *p;
      OR,
int* p;

The statement above defines a pointer variable p. The pointer p holds the memory address. The asterisk is a dereference operator which means pointer toHere pointer p is a pointer to int, that is, it is pointing an integer.

Note: In above statement p is a pointer variable that holds address not *p. The *pis an expression. The content(value) of the memory address pointer p holds is given by expression *p.

Example 2: C++ Pointers

C++ Program to demonstrate the working of pointer.


#include <iostream>
using namespace std;
int main() {
    int *pc, c;
    
    c = 5;
    cout<< "Address of c (&c): " << &c << endl;
    cout<< "Value of c (c): " << c << endl << endl;

    pc = &c;    // Pointer pc holds the memory address of variable c
    cout<< "Address that pointer pc holds (pc): "<< pc << endl;
    cout<< "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
    
    c = 11;    // The content inside memory address &c is changed from 5 to 11.
    cout << "Address pointer pc holds (pc): " << pc << endl;
    cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;

    *pc = 2; 
    cout<< "Address of c (&c): "<< &c <<endl;
    cout<<"Value of c (c): "<< c<<endl<< endl;

    return 0;
}

Output


Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5

Address that pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 5

Address pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 11

Address of c (&c): 0x7fff5fbff80c
Value of c (c): 2

Working of pointer in C++ programming

Explanation of program

  • When c = 5; the value 5 is stored in the address of variable c.
  • When pc = &c; the pointer pc holds the address of c and the expression *pc contains the value of that address which is 5 in this case.
  • When c = 11; the address that pointer pc holds is unchanged. But the expression *pc is changed because now the address &c (which is same as pc) contains 11.
  • When *pc = 2; the content in the address pc(which is equal to &c) is changed from 11 to 2. Since the pointer pc and variable c has address, value of c is changed to 2.